window.onload = function(){
    var myChart = echarts.init(document.getElementById('main'),'macarons');
    // 指定图表的配置项和数据
    var option = {
        tooltip: {
            trigger: 'axis',
            axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
            }
        },
        title:{
            text:"(默认显示当日营收)"
        },
        xAxis: [
            {
                type: 'category',
                data:[],
                axisTick: {
                    alignWithLabel: true
                }
            }
        ],
        yAxis: [
            {
                type: 'value'
            }
        ],
        series: [
            {
                name: '营收',
                type: 'bar',
                barWidth: '60%',
                data:[]
            }
        ]
    };

    // 使用刚指定的配置项和数据显示图表。

//年度收益
    var myChart1 = echarts.init(document.getElementById('main1'),'customed');
    var option1 = {
        title: {
            text: '年度收益'
        },
        tooltip: {
            trigger: 'axis'
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        xAxis: {
            type: 'category',
            boundaryGap: false,
            data: []
        },
        yAxis: {
            type: 'value'
        },
        series: [
            {
                name: '年度收益',
                type: 'line',
                stack: '总量',
                data: []
            }
        ]
    };

    // 使用刚指定的配置项和数据显示图表。

    axios.post("http://localhost:8081/user/common").then(res => {
        var username = document.getElementById("usernamep");
        if(res.data.state){
            var admin = res.data.admin;
            username.innerText = admin.username;
        }
    })
    axios.post("http://localhost:8081/admin/todayProfit").then(res => {
        if(res.data.state){
            var day = new Date().getDate();
            option.xAxis[0].data = [day];
            option.series[0].data = [res.data.result];
            // 使用刚指定的配置项和数据显示图表。
            myChart.setOption(option);
        }
    })
    axios.post("http://localhost:8081/admin/yearProfit").then(res => {
        if(res.data.state){
            option1.xAxis.data = res.data.years;
            option1.series[0].data = res.data.result;
            // 使用刚指定的配置项和数据显示图表。
            myChart1.setOption(option1);
        }
    })

}
function profitin() {
    var money = document.getElementById("getmoney");
    var flag = confirm("确定录入？");
    if(flag){
        axios.post("http://localhost:8081/general_user/profitIn",{profitIn:money.value}).then(res => {
            alert(res.data.msg);
            if(res.data.state){
                location.reload();
            }
        })
    }
}
function run(year) {
    if((year%4===0&&year%100!=0)||(year%400===0)){
        return true;
    }else{
        return false;
    }
}
function dataRange(start,end) {
    var myChart = echarts.init(document.getElementById('main'),'macarons');
    // 指定图表的配置项和数据
    var option = {
        tooltip: {
            trigger: 'axis',
            axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
            }
        },
        title:{
            text:"(默认显示当日营收)"
        },
        xAxis: [
            {
                type: 'category',
                data: [],
                axisTick: {
                    alignWithLabel: true
                }
            }
        ],
        yAxis: [
            {
                type: 'value'
            }
        ],
        series: [
            {
                name: '营收',
                type: 'bar',
                barWidth: '60%',
                data: []
            }
        ]
    };
    var data = [];
    var startYear = start[0]*1000+start[1]*100+start[2]*10+start[3]*1;
    var startMonth = start[5]*10+start[6]*1;
    var startDay = start[8]*10+start[9]*1;

    var endYear = end[0]*1000+end[1]*100+end[2]*10+end[3]*1;
    var endMonth = end[5]*10+end[6]*1;
    var endDay = end[8]*10+end[9]*1;
    var i = 0;
    while(startYear!== endYear||startMonth!==endMonth||startDay!==endDay){
        data[i++] = startDay;
        startDay++;
        if(startMonth === 1 && startDay ===32){
            startMonth++;
            startDay = 1;
        }
        if(startMonth === 2){
            if(run(startYear)&&startDay===30){
                startMonth++;
                startDay = 1;
            }
            if(!run(startYear)&&startDay===29){
                startMonth++;
                startDay = 1;
            }

        }
        if(startMonth === 3 && startDay ===32){
            startMonth++;
            startDay = 1;
        }
        if(startMonth === 4 && startDay ===31){
            startMonth++;
            startDay = 1;
        }
        if(startMonth === 5 && startDay ===32){
            startMonth++;
            startDay = 1;
        }
        if(startMonth === 6 && startDay ===31){
            startMonth++;
            startDay = 1;
        }
        if(startMonth === 7 && startDay ===32){
            startMonth++;
            startDay = 1;
        }
        if(startMonth === 8 && startDay ===32){
            startMonth++;
            startDay = 1;
        }
        if(startMonth === 9 && startDay ===31){
            startMonth++;
            startDay = 1;
        }
        if(startMonth === 10 && startDay ===32){
            startMonth++;
            startDay = 1;
        }
        if(startMonth === 11 && startDay ===31){
            startMonth++;
            startDay = 1;
        }
        if(startMonth === 12 && startDay ===32){
            startMonth++;
            startDay = 1;
        }
        if(startMonth === 13){
            startYear++;
            startMonth = 1;
        }
    }
    data[i] = endDay;
    option.xAxis[0].data = data;
    axios.post("http://localhost:8081/admin/monthProfit",{startTime:start,endTime:end}).then(res => {
        if(res.data.state){
            option.series[0].data = res.data.result;
            myChart.setOption(option);
        }
    })


}